import React from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  TouchableHighlight,
  Image,
} from 'react-native';


import TImage from '../../components/TImage';
import SizeUtils from '../../common/SizeUtils';
import Constants from '../../common/Constants';
import Config from '../../config';
import QZ_QunliaoItemView from './QZ_QunliaoItemView';
import QuanziConstants from './QuanziConstants';
import CommonDataListView from '../common/CommonDataListView';
import QZ_RequestQunliaoListAction from '../../actions/quanzi/QZ_RequestQunliaoListAction';
import ToastUtils from '../../common/ToastUtils';
export default class QZ_QunliaoListiew extends CommonDataListView {
  constructor(props) {
    super(props);

    this._quanziData = this.props.quanziData;
    this._requestType = this.props.requestType;
    this._dataKey = "chatgroup_id";
  }

  componentWillUpdate(nextProps, nextState, nextContext) {
    super.componentWillUpdate(nextProps, nextState, nextContext);
    if(this.props.quanziData != nextProps.quanziData) {
      this._quanziData = nextProps.quanziData;
      this._requestType = nextProps.requestType;
      this._pullRefresh.doRefresh();
    }else if(nextProps.quanziData && nextProps.quanziData.isBack){
      //圈子详情页刷新用
      nextProps.quanziData.isBack = false;
      this._pullRefresh.doRefresh();
    }else if( nextProps.pageData && nextProps.pageData.routeData && nextProps.pageData.routeData.isBack){
       //我的圈子刷新用
      nextProps.pageData.routeData.isBack = false;
      this._pullRefresh.doRefresh();
    }
  }

  doRefresh(){
    this._pullRefresh.doRefresh();
  }



  _onRenderItem(data, index) {
    return (<QZ_QunliaoItemView data={data} isMy={this.props.showCreate != true}/>);
  }

  _onRenderHeader() {
    if(this.props.showCreate != true) {
      return (
        <View style={{height:1}}>
        </View>
      )
    }
    return (
      <TouchableOpacity style={{width:this.props.style.width, paddingTop:SizeUtils.translateDesignHeightToCode(62),
          paddingBottom:SizeUtils.translateDesignHeightToCode(62), flexDirection:"row",
          alignItems:"center", justifyContent:"center", borderColor:"#eeeeee",
          borderBottomWidth:SizeUtils.translateDesignNumToCode(3)}} onPress={this._onPressCreate.bind(this)}>
        <TImage source={require("../../../resource/images/quanzi/groupchat_btn_add.png")}
          style={{width:39, height:39, marginRight:SizeUtils.translateDesignWidthToCode(22)}}/>
        <Text style={{fontSize:SizeUtils.translateFontSizeToCode(32), color:"#333333"}}>
          创建群聊
        </Text>
      </TouchableOpacity>
    )
  }

  _onPressCreate() {
    this.gotoPage(Config.pages.qz_qunliao_create, {quanziData:this.props.quanziData});
  }

  _onPressItem(data) {
    if(this._quanziData && this._quanziData.is_favorited != 1){
        ToastUtils.show("请先关注该圈子");
      return;
    }

    if(data.is_member === 1 &&
        data.state === QuanziConstants.QUN_STATUS.zhengchang) {
      this.gotoPage(Config.pages.qz_qunliao_chat,data);
    }
    else if(data.state === QuanziConstants.QUN_STATUS.shenhe
      || data.is_member === 0) {
      this.gotoPage(Config.pages.quanzi_qun_data,data);
    }
  }

  _requestData(page) {
    if(this.props.requestType == 1) {
      new QZ_RequestQunliaoListAction().call(this, {page:page, requestType:this._requestType});
    }
    else if(this.props.requestType == 2) {
      new QZ_RequestQunliaoListAction().call(this, {page:page, board_id:this._quanziData.board_id, requestType:this._requestType});
    }
  }
}
